package org.wappumobileapp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemClickListener;

import com.thoughtworks.xstream.XStream;

import dataobjects.Category;
import dataobjects.HistoryQuery;
import dataobjects.Message;

public class SendSuggestion extends BaseActivity {

	public static String message = null;
	public static String nick = null;


	private Category selectedCategory;

	// Used to store my posts
	private List<Long> ids = new ArrayList<Long>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.send_suggestion);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.windowtitle);

		final EditText editMessage;

		List<Category> categoryList = Arrays.asList(Category.values());

		// Create a customized ArrayAdapter
		final CategoryArrayAdapter adapter = new CategoryArrayAdapter(
				getApplicationContext(), R.layout.category_listitem,
				categoryList);

		// Get reference to ListView holder
		final ListView lv = (ListView) this.findViewById(R.id.categoryListView);
		lv.setAdapter(adapter);

		// Select listeners
		lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		lv.setClickable(true);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				selectedCategory = adapter.setSelectable(position);
			}
		});
		// Save current selected category.
		selectedCategory = adapter.getSelectedCategory();

		editMessage = (EditText) this.findViewById(R.id.editMessageSG);

		// Get the user nick passed through intent.
		Intent myIntent = getIntent();
		nick = myIntent.getStringExtra("Nick");


		Button buttonMessage = (Button) this.findViewById(R.id.buttonMessageSG);
		// if button is clicked, close the custom dialog
		buttonMessage.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
//				LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
//				Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//				LocationListener ll = new LocationListener() {
//					public void onLocationChanged(Location location) {
//			            updateLocation(location);
//			        }
//
//			        private void updateLocation(Location location) {
//						// TODO Auto-generated method stub
//						
//					}
//
//					public void onStatusChanged(String provider, int status, Bundle extras) {}
//
//			        public void onProviderEnabled(String provider) {}
//
//			        public void onProviderDisabled(String provider) {}
//
//				};
//				
//				lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
//				double longitude = location.getLongitude();
//				double latitude = location.getLatitude();

				

				
				message = editMessage.getText().toString();
	
				
				Message suggestion = GetFromService.addSuggestion(message,
					nick, selectedCategory, null, null); //remember to put longitude,latitude

				if (suggestion == null) {
					Toast.makeText(getApplicationContext(), R.string.Problem,
							Toast.LENGTH_LONG).show();
				} else {

					XStream xstream = new XStream();
					File f = new File(
							"/data/data/org.wappumobileapp/files/my_posts.xml");
					if (f.exists()) {
						try {
							BufferedReader inputReader = new BufferedReader(
									new InputStreamReader(
											openFileInput("my_posts.xml")));
							ids = (List<Long>) xstream.fromXML(inputReader);
						} catch (IOException e) {
							e.printStackTrace();
						}
					}

					ids.add(suggestion.getId());

					writingHistory(ids, "my_posts.xml");

					Toast.makeText(getApplicationContext(), R.string.sendOK,
							Toast.LENGTH_SHORT).show();

					Intent a = new Intent(getApplication(),
							SendSuggestion.class);
					startActivity(a);
					finish();
				}
			}
		});
	}
}